package com.service.Impl;

import com.dto.RoleApplicationDTO;
import com.entity.Account;
import com.entity.RoleApplication;
import com.entity.User;
import com.exception.GlobalException;
import com.mapper.AccountMapper;
import com.mapper.RoleApplicationMapper;
import com.mapper.UserMapper;
import com.service.RoleApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: RoleApplicationServiceImpl
 * Description:
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/06/18 15:48
 */
@Service
public class RoleApplicationServiceImpl implements RoleApplicationService {
    @Autowired
    private RoleApplicationMapper roleApplicationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public void insertRoleApplication(RoleApplication roleApplication) {
        try {
            roleApplicationMapper.insertRoleApplication(roleApplication);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(1001,"添加申请失败");
        }
    }

    @Override
    public List<RoleApplicationDTO> getAllRoleApplications() {
        List<RoleApplication> roleApplications = roleApplicationMapper.getAllRoleApplications();

        List<RoleApplicationDTO> result = new ArrayList<>();
        for (RoleApplication roleApplication : roleApplications) {
            User user = userMapper.selectUserInfoByAccountId(roleApplication.getAccountId());

            RoleApplicationDTO dto = new RoleApplicationDTO(roleApplication, user);
            result.add(dto);
        }
        return result;
    }

    @Override
    public void auditRoleApplication(Integer accountId, Byte status, String rejectReason) {
        try {
            roleApplicationMapper.auditRoleApplication(accountId, status, rejectReason);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(1001,"审核申请失败");
        }
    }
}
